package myservice.android.activities;

import myservice.android.Global;
import myservice.android.R;
import myservice.android.database.Database;
import myservice.android.utilities.EditTextMonitor;
import myservice.android.utilities.SpinnerMonitor;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class PersonActivity extends BaseSaveableActivity {
	
	private Spinner statusSpinner;
	private Button publicationsButton, callsButton, callButton, deleteButton, saveButton;
	private EditText nameText;
	private EditText commentsText;
	private EditText houseApartmentText;
	private EditText streetText;
	private EditText telephoneText;
	private EditText townRegionText;		
	
	private long personId;	         

	@Override
	protected void initializeLayoutIdAndTitle() {
		layoutId = R.layout.person;
		activityTitle = Global.res.getString(R.string.title_person);
	}

	@Override
	protected void retrieveViews() {
        nameText = (EditText) findViewById(R.id.nameText);
        statusSpinner = (Spinner) findViewById(R.id.typeSpinner);       
        commentsText = (EditText) findViewById(R.id.commentsText);
        houseApartmentText = (EditText) findViewById(R.id.houseApartmentText);
        streetText = (EditText) findViewById(R.id.streetText);
        townRegionText = (EditText) findViewById(R.id.townRegionText);
        telephoneText = (EditText) findViewById(R.id.telephoneText);         
        publicationsButton = (Button) findViewById(R.id.publicationsButton);
        callsButton = (Button) findViewById(R.id.callsButton);  
        callButton = (Button) findViewById(R.id.callButton);     
        deleteButton = (Button) findViewById(R.id.deleteButton);	
        saveButton = (Button) findViewById(R.id.saveButton);
	}
	
	@Override
	protected void loadActivityParameters() {		
		personId = activityParameters.getLong("person_id");		
	}	

	@Override
	protected void assignListeners() {
			
		nameText.addTextChangedListener(new EditTextMonitor(this));
        statusSpinner.setOnItemSelectedListener(new SpinnerMonitor(this));  
		commentsText.addTextChangedListener(new EditTextMonitor(this));
		
        publicationsButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				save();
				Intent i = new Intent(getApplicationContext(), PublicationListActivity.class);
				i.putExtra("person_id",personId);			
				startActivity(i);
			}        	
        });
        
        saveButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				save();
				finish();
			}
        });        
        
        deleteButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(PersonActivity.this);
				alertDialogBuilder.setTitle(Global.res.getText(R.string.title_warning));				
				alertDialogBuilder
					.setMessage(Global.res.getText(R.string.message_delete_person_confirmation))
					.setPositiveButton(Global.res.getText(R.string.button_yes),new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,int id) {
							Database.deleteInterestedPerson(personId);
							finish();
						}
					  })
					.setNegativeButton(Global.res.getText(R.string.button_no),new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							dialog.cancel();
						}
					});
		 
				AlertDialog alertDialog = alertDialogBuilder.create();
				alertDialog.show();				
			}        	
        });
        
        callsButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				save();
				Intent i = new Intent(getApplicationContext(), PersonCallListActivity.class);
				i.putExtra("person_id",personId);			
				startActivity(i);
			}        	
        });  
                
		// Check if telephony is available; hide the button if not
		PackageManager pm = this.getPackageManager();
		if (!pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY))        
			callButton.setVisibility(View.GONE);
		else		
	        callButton.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					
					if (telephoneText.length()<1)
						return;
					
					Intent intent = new Intent(Intent.ACTION_CALL);
	
					intent.setData(Uri.parse("tel:" + telephoneText.getText()));
					startActivity(intent);
				}
				
			});  
        
        // Tooltips
        publicationsButton.setOnLongClickListener(new OnLongClickListener() {
			@Override
			public boolean onLongClick(View button) {
				Global.displayToast(PersonActivity.this, Global.res.getString(R.string.tip_owned_publications));
				return true;
			}        	
        });         
        callsButton.setOnLongClickListener(new OnLongClickListener() {
			@Override
			public boolean onLongClick(View button) {
				Global.displayToast(PersonActivity.this, Global.res.getString(R.string.tip_calls));
				return true;
			}        	
        });		
	}
	   
    @Override
    protected void updateUi() {    	
    	   	  	    		    	                	        	
        Cursor cursor = Database.select("SELECT * FROM persons WHERE personid="+personId);
        cursor.moveToNext();
        String s = cursor.getString(cursor.getColumnIndex("name"));
        if (s!=null) nameText.setText(s);
        statusSpinner.setSelection(cursor.getInt(cursor.getColumnIndex("status")));
        s = cursor.getString(cursor.getColumnIndex("comments"));
        if (s!=null) commentsText.setText(s);
        s = cursor.getString(cursor.getColumnIndex("houseapartment"));
        if (s!=null) houseApartmentText.setText(s);     
        s = cursor.getString(cursor.getColumnIndex("street"));
        if (s!=null) streetText.setText(s);  
        s = cursor.getString(cursor.getColumnIndex("townregion"));
        if (s!=null) townRegionText.setText(s);      
        s = cursor.getString(cursor.getColumnIndex("telephone"));
        if (s!=null) telephoneText.setText(s);         		
     	
        cursor.close();  
        
        activityContentModified = false;
        
    }	

    @Override
    protected void save() {				
		Database.execSQL("UPDATE persons SET name='"+Global.fixText(nameText.getText())+"', status="+statusSpinner.getSelectedItemId()+", comments='"
				+Global.fixText(commentsText.getText())+"', houseapartment='"+Global.fixText(houseApartmentText.getText())+"', street='"+Global.fixText(streetText.getText())+
				"', townregion='"+Global.fixText(townRegionText.getText())+"', telephone='"+Global.fixText(telephoneText.getText())+"', timestamp="+Global.getTimeStamp()+
				" WHERE personid="+personId);    	
    }         

}
